﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using EntityObjects;

namespace DAL_V1.App_Code.DBBroker
{
    public class CollectionPointDBBroker
    {
        private SSISDataSet ds;
        private SSISDataSetTableAdapters.CollectionPointTableAdapter tda;
        
        
        private int index;
        //private List<CollectionPoint> cp_list;
        private CollectionPoint cp;
        private SSISDataSet.CollectionPointDataTable cp_td;
       
        //singleton
        private static bool instance_flag = false;
        private static CollectionPointDBBroker instance;

        private CollectionPointDBBroker()
        {
            ds = new SSISDataSet();
            tda = new SSISDataSetTableAdapters.CollectionPointTableAdapter();
            tda.Fill(ds.CollectionPoint);
        }

        public static CollectionPointDBBroker getCollectionPointDBBroker()
        {
            if (!instance_flag)
            {
                instance = new CollectionPointDBBroker();
                instance_flag = true;
               
                return instance;
            }
            else
            {
                return instance;
            }
        }

        public List<CollectionPoint> selectAllCollectionPoint()
        {
            List<CollectionPoint> cp_list = new List<CollectionPoint>();
                        
            foreach (SSISDataSet.CollectionPointRow r in ds.CollectionPoint.Rows)
            {
                CollectionPoint cp = new CollectionPoint();
                cp.CollectionId = r.CollectionId;
                cp.Place = r.Place;
                cp.Time = r.Time;
                cp_list.Add(cp);
            }
            return cp_list;
        }

        public DataTable selectAllCollectionPoint_DT()
        {
            return ds.CollectionPoint;
        }
       

        public int addCollectionPoint(CollectionPoint collectionPoint)
        {
            SSISDataSet.CollectionPointRow newRow = ds.CollectionPoint.NewCollectionPointRow();
            newRow.CollectionId = collectionPoint.CollectionId;
            newRow.Place = collectionPoint.Place;
            newRow.Time = collectionPoint.Time;
                    
            ds.CollectionPoint.Rows.Add(newRow);           
            return tda.Update(ds.CollectionPoint);
        }

        //private int getIndex(int Id)
        //{
        //    SSISDataSet.CollectionPointRow row = ds.CollectionPoint.FindByCollectionId(Id);
        //    return ds.CollectionPoint.Rows.IndexOf(row);
        //}

        private SSISDataSet.CollectionPointRow getRowById(int Id)
        {
            SSISDataSet.CollectionPointRow row=ds.CollectionPoint.FindByCollectionId(Id);
            return row;
        }

        public int updateCollectionPoint(CollectionPoint collectionPoint, int Id)
        {
            SSISDataSet.CollectionPointRow row = getRowById(Id);
            if (row != null)
            {
                row.CollectionId = collectionPoint.CollectionId;
                row.Place = collectionPoint.Place;
                row.Time = collectionPoint.Time;
            }
            return tda.Update(ds.CollectionPoint);
        }

        public int deleteCollectionPoint(int Id)
        {
            SSISDataSet.CollectionPointRow row = getRowById(Id);
            if (row != null)
            {
                ds.CollectionPoint.Rows.Remove(row);
            }            
            return tda.Update(ds.CollectionPoint);
        }

        public CollectionPoint selectCollectionPoint(int Id)
        {
            SSISDataSet.CollectionPointRow row = getRowById(Id);

            if (row != null)
            {
                CollectionPoint cp = new CollectionPoint();
                cp.CollectionId = row.CollectionId;
                cp.Place = row.Place;
                cp.Time = row.Time;

                return cp;
            }
            else
            {
                return null;
            }
        }
    }
}
